package com.lanyan.system.service.impl;

import java.util.List;
import java.util.stream.Collectors;
        import com.lanyan.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.lanyan.common.utils.StringUtils;
import com.lanyan.system.mapper.ShoppingMapper;
import com.lanyan.system.domain.Shopping;
import com.lanyan.system.domain.vo.ShoppingVo;
import com.lanyan.system.service.IShoppingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 购物车Service业务层处理
 *
 * @author lanyan
 * @date 2024-11-26
 */
@Service
public class ShoppingServiceImpl  extends ServiceImpl<ShoppingMapper,Shopping> implements IShoppingService

{
    @Autowired
    private ShoppingMapper shoppingMapper;

    /**
     * 查询购物车
     *
     * @param shoppingId 购物车主键
     * @return 购物车
     */
    @Override
    public Shopping selectShoppingByShoppingId(Long shoppingId)
    {
        return this.getById(shoppingId);
    }

    /**
     * 查询购物车Vo列表
     *
     * @param shopping 购物车
     * @return 购物车
     */
    @Override
    public List<ShoppingVo> selectShoppingVoList(Shopping shopping)
    {
        return selectShoppingList(shopping)
                .stream()
                .map(x -> (ShoppingVo)x)
                .collect(Collectors.toList());
    }
    /**
     * 查询购物车列表
     *
     * @param shopping 购物车
     * @return 购物车
     */
    @Override
    public List<Shopping> selectShoppingList(Shopping shopping)
    {
        LambdaQueryChainWrapper<Shopping> wrapper = this.lambdaQuery().eq(Shopping::getDelFlag,"0");
        if (shopping.getAccountId() != null) {
            wrapper.eq(Shopping::getAccountId, shopping.getAccountId());
        }
        if (shopping.getProductId() != null) {
            wrapper.eq(Shopping::getProductId, shopping.getProductId());
        }
        if (shopping.getNum() != null) {
            wrapper.eq(Shopping::getNum, shopping.getNum());
        }
        return wrapper.list();
    }

        /**
         * 新增购物车
         *
         * @param shopping 购物车
         * @return 结果
         */
    @Override
        public boolean insertShopping(Shopping shopping)
        {
                    shopping.setCreateTime(DateUtils.getNowDate());
                return this.save(shopping);
        }

        /**
         * 修改购物车
         *
         * @param shopping 购物车
         * @return 结果
         */
    @Override
        public boolean updateShopping(Shopping shopping)
        {
                    shopping.setUpdateTime(DateUtils.getNowDate());
            return this.updateById(shopping);
        }

        /**
         * 批量删除购物车
         *
         * @param shoppingIds 需要删除的购物车主键
         * @return 结果
         */
    @Override
        public boolean deleteShoppingByShoppingIds(Long[] shoppingIds)
        {
            return this.lambdaUpdate().set(Shopping::getDelFlag,"1").in(Shopping::getShoppingId,shoppingIds).update();
        }

        /**
         * 删除购物车信息
         *
         * @param shoppingId 购物车主键
         * @return 结果
         */
    @Override
        public boolean deleteShoppingByShoppingId(Long shoppingId)
        {
            return this.lambdaUpdate().set(Shopping::getDelFlag,"1").eq(Shopping::getShoppingId,shoppingId).update();
        }
    }
